package com.roc.web.thread;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.roc.pojo.Operators;
import com.roc.pojo.TblEditMchtInf;
import com.roc.service.IMerchantServices;
import com.roc.service.IUserService;
import com.roc.service.ServiceException;
import com.roc.web.action.merinfo.merinfoAction;

/**
*@auhor   zbl
*@date    2017年2月20日 上午11:20:48
*@desc    说明 商户审核线程
**/
@Component
public class MerinfoCheckThread extends Thread {
	//private static final long serialVersionUID = 2195079722029876684L;

	private  static Logger logger = Logger.getLogger(merinfoAction.class);
	
	@Autowired
	private IMerchantServices merinfoService;
	
	@Autowired
	private IUserService  userService;
	
	@Override
	public void run() {
		boolean ip=getCurrentIpAddress();
		if(ip){
			int i=0;
			boolean isCheck=false;
			Operators op  = new Operators();
			op.setId(40002);
			op.setRealName("cj");
			while(true){
				try {
					logger.info("------开始第["+i+"]次执行审核线程------");
					 isCheck=userService.isCheckMerinfo();
					if(isCheck==true){
						
						List<?> list=merinfoService.queryNeedCheckMerinfo(100);
						logger.info("----批量审核开关开启开始第{"+i+"}次批量审核,本次审核共计{"+list.size()+"}条----");
						if(null!=list && list.size()>0){
							Iterator<?> itor= list.iterator();
							while(itor.hasNext()){
								TblEditMchtInf merinfoTmp=(TblEditMchtInf)itor.next();
								merinfoService.checkMerinfo(merinfoTmp,op);
							}
						}else{
							isCheck=false;
						}
						//isCheck=merinfoService.checkMerinfo(100);
					}
					logger.info("------结束第["+i+"]次执行审核线程------");
					i++;
				} catch (ServiceException e) {
					logger.error(e.getMessage(),e);
				}finally {
					try {
						if(isCheck==false){
							Thread.sleep(1000*60*6);
						}else{
							Thread.sleep(1000*60*2);
						}
						
					} catch (InterruptedException e) {
						logger.error("商户批量审核线程强制等待1分钟报错",e);
					}
				}
			}
		
		}else{
			logger.info("--非生产130ip--");
		}
	}
	  
	private static boolean getCurrentIpAddress(){
		String result="";
		try {
			//HttpServletRequest request=
		//	System.out.println("端口"+request.getLocalPort());
			Enumeration<?> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
			InetAddress ip = null;
			while (allNetInterfaces.hasMoreElements()){
				NetworkInterface netInterface = (NetworkInterface) allNetInterfaces.nextElement();
				Enumeration addresses = netInterface.getInetAddresses();
				while (addresses.hasMoreElements()){
					ip = (InetAddress) addresses.nextElement();
					if (ip != null && ip instanceof Inet4Address){
						if(ip.getHostAddress().equals("173.160.201.130")){
							return true;
						}
					logger.info("本机的IP = " + ip.getHostAddress());
					}
				}
			}
			return false;
		} catch (SocketException e) {
			logger.error("获取本地地址失败",e);
		}
		return false;
	}
}
